package class06;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @Auther: xw
 * @Create: 2022/3/21 15:19
 * @Description:
 */
public class ShowComparator {

    public static class Student {
        private Integer id;
        private String name;
        private Integer age;
        public Student() {
        }

        public Student(Integer id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" + "id=" + id +  ", name='" + name + '\'' +", age=" + age + '}';
        }
    }

    public static class compareStudentById implements Comparator<Student> {
        /**
         *  如果返回负数，认为第一个数应该排在前面
         *  如果返回正数，则认为第二个数应该排在前面
         *  如果返回零，则认为谁排前面无所谓
         * @param o1
         * @param o2
         * @return
         */
        public int compare(Student o1, Student o2) {
            if(o1.id < o2.id){
                return -1;
            }else if(o1.id > o2.id){
                return 1;
            }else{
                return 0;
            }
        }
    }

    public static class MyComparator implements Comparator<Integer>{

        @Override
        public int compare(Integer o1, Integer o2) {
            if(o1 < o2){
                return 1;
            }else if(o1 > o2){
                return -1;
            }else {
                return 0;
            }
        }
    }
    public static void main(String[] args) {
        //优先级队列
        PriorityQueue<Integer> queue = new PriorityQueue<>(new MyComparator());
        queue.add(6);
        queue.add(4);
        queue.add(7);
        queue.add(9);
        queue.add(1);
        System.out.println(queue.peek());
        queue.add(0);
        System.out.println(queue.peek());
        System.out.println("--------------------");
        while (!queue.isEmpty()){
            System.out.println(queue.poll());
        }

        System.out.println("=======================");
        PriorityQueue<Student> students = new PriorityQueue<>(new compareStudentById());
        Student s1 = new Student(2,"张三",27);
        Student s2 = new Student(1,"李四",29);
        Student s3 = new Student(5,"王五",12);
        Student s4 = new Student(3,"赵六",44);
        Student s5 = new Student(4,"孙七",25);
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
//        System.out.println(students.peek());
        students.add(s5);
        while (!students.isEmpty()){
            System.out.println(students.poll());
        }
    }
}
